package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjInformation;
import com.ruoyi.cngj.service.ICngjInformationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资料下载Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/information")
public class CngjInformationController extends BaseController
{
    private String prefix = "cngj/information";

    @Autowired
    private ICngjInformationService cngjInformationService;

    //@RequiresPermissions("cngj:information:view")
    @GetMapping()
    public String information()
    {
        return prefix + "/information";
    }

    /**
     * 查询资料下载列表
     */
    //@RequiresPermissions("cngj:information:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjInformation cngjInformation)
    {
        startPage();
        List<CngjInformation> list = cngjInformationService.selectCngjInformationList(cngjInformation);
        return getDataTable(list);
    }

    /**
     * 导出资料下载列表
     */
    //@RequiresPermissions("cngj:information:export")
    @Log(title = "资料下载", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjInformation cngjInformation)
    {
        List<CngjInformation> list = cngjInformationService.selectCngjInformationList(cngjInformation);
        ExcelUtil<CngjInformation> util = new ExcelUtil<CngjInformation>(CngjInformation.class);
        return util.exportExcel(list, "资料下载数据");
    }

    /**
     * 新增资料下载
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资料下载
     */
    //@RequiresPermissions("cngj:information:add")
    @Log(title = "资料下载", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjInformation cngjInformation)
    {
        return toAjax(cngjInformationService.insertCngjInformation(cngjInformation));
    }

    /**
     * 修改资料下载
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjInformation cngjInformation = cngjInformationService.selectCngjInformationById(id);
        mmap.put("cngjInformation", cngjInformation);
        return prefix + "/edit";
    }

    /**
     * 修改保存资料下载
     */
    //@RequiresPermissions("cngj:information:edit")
    @Log(title = "资料下载", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjInformation cngjInformation)
    {
        return toAjax(cngjInformationService.updateCngjInformation(cngjInformation));
    }

    /**
     * 删除资料下载
     */
    //@RequiresPermissions("cngj:information:remove")
    @Log(title = "资料下载", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjInformationService.deleteCngjInformationByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/informationMove")
    @ResponseBody
    public AjaxResult informationMove(String type,String id,String changeId)
    {
        CngjInformation cngjInformation = cngjInformationService.selectCngjInformationById(id);
        CngjInformation changeCngiInformation = cngjInformationService.selectCngjInformationById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjInformation.getInformationSort();
            cngjInformation.setInformationSort(changeCngiInformation.getInformationSort());
            changeCngiInformation.setInformationSort(currSort);
            num +=cngjInformationService.updateCngjInformation(cngjInformation);
            num +=cngjInformationService.updateCngjInformation(changeCngiInformation);
        }
        if("3".equals(type)){
            cngjInformation.setInformationSort(Integer.parseInt(changeId)+1);
            num +=cngjInformationService.updateCngjInformation(cngjInformation);
        }
        return toAjax(num);
    }

    /**
     * 启用停用banner素材
     */
    @Log(title = "启用停用资料下载素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String informationState)
    {
        CngjInformation cngjInformation = new CngjInformation();
        cngjInformation.setId(id);
        cngjInformation.setInformationState(informationState);
        return toAjax(cngjInformationService.updateCngjInformation(cngjInformation));
    }

}
